//
//  HomeController.swift
//  SkyApp
//
//  Created by 白翊 on 2021/12/20.
//

import UIKit
import SnapKit
import FDFullscreenPopGesture
import YYKit

class HomeController: BaseViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        fd_prefersNavigationBarHidden = true

        setupUI()
        playBullet()
        
        view.addGestureRecognizer(UITapGestureRecognizer.init(actionBlock: { _ in
            self.view.endEditing(true)
            if self.bottomView.isHidden {
                self.bottomView.isHidden = false
                UIView.animate(withDuration: 0.25) {
                    self.bottomView.alpha = 1
                }
            }else {
                UIView.animate(withDuration: 0.25) {
                    self.bottomView.alpha = 0
                } completion: { _ in
                    self.bottomView.isHidden = true
                }
            }
        }))
        
        YYTextKeyboardManager.default()?.add(self)
    }
    
    deinit {
        YYTextKeyboardManager.default()?.remove(self)
    }
    
    private func setupUI() {
        view.addSubview(bulletLabel)
        view.addSubview(bottomView)
        
        bottomView.snp.makeConstraints { make in
            make.left.right.equalTo(0)
            make.bottom.equalTo(0)
            make.height.equalTo(80)
        }
    }
    
    private func playBullet() {
        view.backgroundColor = bulletBGColor
        bulletLabel.transform = .identity
        bulletLabel.layer.removeAnimation(forKey: "animation")

        let shadow = NSShadow()
        shadow.shadowOffset = .zero
        shadow.shadowBlurRadius = bulletWeight.shadowBlur
        shadow.shadowColor = bulletShadow
        
        let font = UIFont.systemFont(ofSize: bulletSize.size, weight: bulletWeight.weight)

        bulletLabel.attributedText = NSAttributedString(string: bulletDesc, attributes: [
            .shadow: shadow,
            .foregroundColor: bulletColor,
            .font: font
        ])

        if bulletStop {
            bulletLabel.numberOfLines = 0
            bulletLabel.adjustsFontSizeToFitWidth = true
            
            let interval = STATUEBAR_HEIGHT + interval
            
            let maxWidth = HEIGHT - interval * 2
            let maxHeight = WIDTH - interval
            bulletLabel.textAlignment = (bulletDesc as NSString).width(for: font) < maxWidth ? .center : .justified

            bulletLabel.frame = CGRect(x: (WIDTH - maxWidth) * 0.5, y: (HEIGHT - maxHeight) * 0.5, width: maxWidth, height: maxHeight)
            bulletLabel.transform = .init(rotationAngle: .pi * 0.5)

        }else {
            bulletLabel.numberOfLines = 1
            bulletLabel.adjustsFontSizeToFitWidth = false
            
            let width = (bulletDesc as NSString).width(for: font)
            let height = (bulletDesc as NSString).height(for: font, width: CGFloat.greatestFiniteMagnitude)
            bulletLabel.frame = CGRect(x: (WIDTH - width) * 0.5, y: (HEIGHT - height) * 0.5, width: width, height: height)
            bulletLabel.transform = .init(rotationAngle: .pi * 0.5)
            
            let animation = CABasicAnimation(keyPath: "position.y")
            animation.fromValue = HEIGHT + width * 0.5
            animation.toValue = -width * 0.5
            animation.duration = (HEIGHT + width) / bulletSpeed.speed
            animation.repeatCount = MAXFLOAT
            animation.isRemovedOnCompletion = false
            animation.fillMode = .forwards
            bulletLabel.layer.add(animation, forKey: "animation")
        }
    }
    
    lazy var bulletLabel = UILabel()

    lazy var bottomView: BulletInputView = {
        $0.delegate = self
        return $0
    }(BulletInputView())
}

extension HomeController: BulletInputViewProtocol {
    func updateBullet() {
        playBullet()
    }
}

extension HomeController: YYTextKeyboardObserver {
    func keyboardChanged(with transition: YYTextKeyboardTransition) {
        let height: CGFloat = transition.toVisible.boolValue ? transition.toFrame.size.height : 0
        bottomView.snp.updateConstraints { make in
            make.bottom.equalTo(-height)
        }
        
        UIView.animate(withDuration: transition.animationDuration) {
            self.view.layoutIfNeeded()
        }
    }
}
